gameUtilID = 'gameUtilOther';
var rightClickCache = gameContentID;

var focusJSRetries;
var LINUX_MAX_FOCUS_EDITOR_RETRIES = 10;
var LINUX_FOCUS_EDITOR_RETRY_INTERVAL = 100;	// msec



function MZLoad()
{
	gameUtilJS = document.getElementById(gameUtilID);
	gameContentJS = document.getElementById(gameContentID);
	gameContainerJS = document.getElementById(gameContainerID);
	
	//gameUtilJS.style.visibility = "hidden";
	gameUtilJS.contentWindow.document.body.innerHTML = 'a';
	gameUtilJS.contentWindow.document.designMode = "on";
	// Game Util Listeners
	gameUtilJS.contentWindow.addEventListener('keydown', MZKeyDownHandler, false);
	gameUtilJS.contentWindow.addEventListener('keypress', MZKeyPressHandler, false);
	gameUtilJS.contentWindow.addEventListener('keyup', MZKeyUpHandler, false);
	//gameUtilJS.contentWindow.addEventListener('focus', MZFocusHandler, true);
	//gameUtilJS.contentWindow.addEventListener('blur', MZBlurHandler, true);
	gameUtilJS.contentWindow.addEventListener('mousedown', MZOnRightClick, true);
	gameContentJS.addEventListener('mousedown', MZOnRightClick, true);
	
	window.addEventListener('mousedown', MZOnRightClick, true);
	//window.addEventListener("DOMMouseScroll", MZWheel, false);
}

function MZChangeGameSize(width, height)
{
	gameContentJS.setAttribute("width", width);
	gameContentJS.setAttribute("height", height);
	window.resizeBy(width - prevWidth, height - prevHeight );
	prevWidth = width;
	prevHeight = height;
}

function MZKeyPressHandler(oEvent)
{
	var nKeyCode = oEvent.keyCode ? oEvent.keyCode : oEvent.which ? oEvent.which : void 0;
	var sChar = String.fromCharCode(nKeyCode).toLowerCase();
	var bCtrlPressed = oEvent.ctrlKey || oEvent.metaKey;
	var bAltPressed = oEvent.altKey;
	if (bCtrlPressed && sChar=="v")
	{
		gameUtilJS.contentWindow.document.body.innerHTML = '';
		setTimeout("MZOnAfterPaste();", 0);
	}
	else if (bCtrlPressed && sChar=="c")
	{
		gameUtilJS.contentWindow.document.body.innerHTML = gameContentJS.onBrowserCopy();
		gameUtilJS.contentWindow.document.execCommand('SelectAll', false, null);
	}
	else if (bCtrlPressed && sChar=="x")
	{
		gameUtilJS.contentWindow.document.body.innerHTML = gameContentJS.onBrowserCut();
		gameUtilJS.contentWindow.document.execCommand('SelectAll', false, null);
		setTimeout("MZOnAfterCut();", 0);
	}
	else if (!(bCtrlPressed && sChar=="t"))
	{
		var keyCode = oEvent.keyCode;
		if (oEvent.charCode == 32 )
			keyCode = 32;	// Flex buttons expect a keycode here
		try {
			gameContentJS.onKeyPressMZ(oEvent.charCode, keyCode, oEvent.ctrlKey, oEvent.altKey, oEvent.shiftKey);
		} catch (e) {
		}
		if (gameUtilJS.contentWindow.document.body.innerHTML == '<br>' || gameUtilJS.contentWindow.document.body.innerHTML.length > 10)
			gameUtilJS.contentWindow.document.body.innerHTML = 'a';
	}
	if (MZPreventDefault(bCtrlPressed, bAltPressed, sChar))
		oEvent.preventDefault();
}

function MZKeyDownHandler(oEvent)
{
	var keyCode = oEvent.keyCode;
	
	// propagate key-down events for ctrl/shift/alt so we can track state
	if (keyCode == 16 || keyCode == 17 || keyCode == 18)
	{
		try {
			gameContentJS.onKeyDownMZ(oEvent.keyCode, oEvent.ctrlKey, oEvent.altKey, oEvent.shiftKey);
		} catch (e) {
		}
	}
}

function MZKeyUpHandler(oEvent)
{
	try {
		gameContentJS.onKeyUpMZ(oEvent.charCode, oEvent.keyCode, oEvent.ctrlKey, oEvent.altKey, oEvent.shiftKey);
	} catch (e) {
	}
}


function MZPreventDefault(bCtrlPressed, bAltPressed, sChar)
{
	if (bAltPressed)
		return true;	// to support Alt+XXXX Unicode keystroke sequences
		
	if (sChar == '\t')
		return true;
	if (bCtrlPressed)
	{
		if (sChar == 'c' || sChar == 'v' || sChar == 'x' || sChar == 't')
			return false;
		else
			return true;
	}
	return false;
}

function MZReturnFocusToJS()
{
	var result = true;
	try
	{
		if (isWin)
		{
			gameUtilJS.contentWindow.focus();
		}
		else if (isLin)
		{
			focusJSRetries = 0;
			MZReturnFocusToEditorLinux();
		}
	}
	catch (e)
	{
		if (e.name == "TypeError")
		{
			if (typeof(gameContentJS) == "object")
				setTimeout('gameContentJS.focus();', 0);
			result = false;
		}
	}
	return result;
}

function MZOnRightClick(ev)
{
	if (ev.button != 0) 
	{
		MZKillEvents(ev);
		if(ev.target.id == gameContentID && rightClickCache == gameContentID) {
	   		gameContentJS.openRightClick();
		}
		rightClickCache = ev.target.id;
	}
}

function MZKillEvents (eventObject) 
{
	if(eventObject) 
	{
		if (eventObject.stopPropagation) eventObject.stopPropagation();
		if (eventObject.preventDefault) eventObject.preventDefault();
	}
}


function MZReturnFocusToEditorLinux()
{
	gameUtilJS.contentWindow.focus();
	if (focusJSRetries < LINUX_MAX_FOCUS_EDITOR_RETRIES)
		setTimeout(MZReturnFocusToEditorLinux, (focusJSRetries++)*LINUX_FOCUS_EDITOR_RETRY_INTERVAL);
}

function MZReturnFocusToCMM()
{
	// This doesn't work. Apparently the Mozilla plugin API doesn't allow programatically setting focus to a plugin (Flash in this case).
	// It actually causes problems such as requiring two clicks to focus in a text box -syost
	// setTimeout('document.getElementById(appName).focus();', 0);
}

function MZWheel(event)
{
     var delta = 0;
    if (event.detail)
        delta = -event.detail;
    delta = delta * 6;
//    doTrace('delta='+delta);
     if (delta) {
          gameContentJS.onMouseWheel(delta);
     }
}